<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="org.apak.model.*" import="org.apak.control.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Service action</title>
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
		<script type="text/javascript" src="./js/serviceType.js"></script>
</head>
<body>
<%
	ServiceAction serviceAction = (ServiceAction) request.getAttribute("serviceAction");
	ServiceActionStatusType[] serviceActionStatusTypeArray = (ServiceActionStatusType[]) request.getAttribute("serviceActionStatusTypeArray");
	ServiceType[] serviceTypeArray = (ServiceType[]) request.getAttribute("serviceTypeArray");
	Device[] deviceArray = (Device[]) request.getAttribute("deviceArray");
	ErrorList errors = (ErrorList) request.getAttribute("errors");
	String success = (String) request.getAttribute("success");
	String serviceOrderID = (String) request.getAttribute("serviceOrderID");
	ServiceActionForm serviceActionForm = (ServiceActionForm) request.getAttribute("serviceActionForm");
	String[] fields = {"", "0", "", "", "0", "0"};
	String[] errorsText = {"", "", "", "", "", ""};
	if(errors != null) {
		serviceActionForm = (ServiceActionForm) errors.getForm();
		fields[0] = serviceActionForm.getAction_description();
		fields[1] = serviceActionForm.getService_type_fk();
		fields[2] = serviceActionForm.getService_amount();
		fields[3] = serviceActionForm.getPrice();
		fields[4] = serviceActionForm.getService_action_status_type_fk();
		fields[5] = serviceActionForm.getService_device_fk();
		errorsText[0] = errors.getError("action_description");
		errorsText[2] = errors.getError("service_amount");
		errorsText[3] = errors.getError("price");
	} else if(serviceActionForm != null) {
		fields[0] = serviceActionForm.getAction_description();
		fields[1] = serviceActionForm.getService_type_fk();
		fields[2] = serviceActionForm.getService_amount();
		fields[3] = serviceActionForm.getPrice();
		fields[4] = serviceActionForm.getService_action_status_type_fk();
		fields[5] = serviceActionForm.getService_device_fk();
	}  else if(serviceAction != null) {
		fields[0] = serviceAction.getAction_description();
		fields[1] = Integer.toString(serviceAction.getService_type_fk());
		fields[2] = Integer.toString(serviceAction.getService_amount());
		fields[3] = Integer.toString(serviceAction.getPrice());
		fields[4] = Integer.toString(serviceAction.getService_action_status_type_fk());
		fields[5] = Integer.toString(serviceAction.getService_device_fk());
	}
	if(success == null)
		success = "";
%>
<%
	if(serviceOrderID != null) {
%>
<a href="./serviceOrder?id=<%= serviceOrderID %>">... back to service order</a>
<%
	}
%>
<form action='serviceAction?action=save' method='POST'>
	<table>
		<tr>
			<td>Action Description</td>
			<td><textarea name="actionDescription"><%= fields[0] %></textarea> <%= errorsText[0] %></td>
		</tr>
		<tr>
			<td>Status</td>
			<td>
				<select name="action_status">
<%
				if(serviceActionStatusTypeArray != null)
				for(int k = 0; k < serviceActionStatusTypeArray.length; k++) {
					ServiceActionStatusType statusType = serviceActionStatusTypeArray[k];
					if(statusType.getService_action_status_type() == Integer.parseInt(fields[4])) {
%>
						<option value="<%= statusType.getService_action_status_type() %>" selected><%= statusType.getType_name() %></option>
<%	
					} else {
%>
						<option value="<%= statusType.getService_action_status_type() %>"><%= statusType.getType_name() %></option>
<%	
					}
				}
%>
			</select>
		</td>
		<tr>
			<td>Type</td>
			<td>
				<select name="action_type" id="action_type">
<%
				if(serviceTypeArray != null)
				for(int k = 0; k < serviceTypeArray.length; k++) {
					ServiceType serviceType = serviceTypeArray[k];
					if(serviceType.getService_type() == Integer.parseInt(fields[1])) {
%>
						<option value="<%= serviceType.getService_type() %>" selected><%= serviceType.getType_name() %></option>
<%	
					} else {
%>
						<option value="<%= serviceType.getService_type() %>"><%= serviceType.getType_name() %></option>
<%	
					}
				}
%>
			</select>
		</td>
		</tr>
		<tr>
			<td>Device</td>
			<td>
				<select name="device">
			<%
				if(fields[5].equals("0")) {
			%>
				<option value="0" selected>Select device</option>
			<%
				} else {
			%>
				<option value="0">Select device</option>
<%				
				}
				if(deviceArray != null)
				for(int k = 0; k < deviceArray.length; k++) {
					Device device = deviceArray[k];
					if(fields[5].equals(Integer.toString(device.getDevice()))) {
%>
						<option value="<%= device.getDevice() %>" selected><%= device.getName() %>, <%= device.getModel() %></option>
				<%
					}
					else {
				%>
						<option value="<%= device.getDevice() %>"><%= device.getName() %>, <%= device.getModel() %></option>
				<%
					}
				%>
<%	
				}
%>
			</select>
		</td>
		</tr>
		<tr>
			<td>Service amount</td>
			<td><input type="text" name="amount" value="<%= fields[2] %>"><%= errorsText[2] %></td>
		</tr>
		<tr>
			<td>Price</td>
			<td><input type="text" name="price" id="price" value="<%= fields[3] %>"><%= errorsText[3] %></td>
		</tr>
	</table>
	<input type="submit" name="actionButton" value="Save"/> <%= success %>
</form>
</body>
</html>